package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZhuanjiabiandongshenbaoMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 专家变动申报Service业务层处理
 * 
 * @author dy
 * @date 2024-05-27
 */
@Service
public class ZhuanjiabiandongshenbaoServiceImpl implements IZhuanjiabiandongshenbaoService 
{
    @Autowired
    private ZhuanjiabiandongshenbaoMapper zhuanjiabiandongshenbaoMapper;

    @Autowired
    private IZhuanjiabiandongshenbaoQuxiaozigeService quxiaozigeService;

    @Autowired
    private IZhuanjiabiandongshenbaoChuguoService chuguoService;

    @Autowired
    private IZhuanjiabiandongshenbaoLizhiService lizhiService;

    @Autowired
    private IZhuanjiabiandongshenbaoShilianService shilianService;

    @Autowired
    private IZhuanjiabiandongshenbaoTingfaService tingfaService;

    @Autowired
    private IZhuanjiabiandongshenbaoQushiService qushiService;

    @Autowired
    private IZhuanjiabiandongshenbaoHuifuService huifuService;

    @Autowired
    private IZhuanjiabiandongshenbaoZhuanliService zhuanliService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IShiwuService shiwuService;

    @Autowired
    private IFujianService fujianService;

    /**
     * 查询专家变动申报
     * 
     * @param shenbaoid 专家变动申报主键
     * @return 专家变动申报
     */
    @Override
    public Zhuanjiabiandongshenbao selectZhuanjiabiandongshenbaoByShenbaoid(String shenbaoid)
    {
        return zhuanjiabiandongshenbaoMapper.selectZhuanjiabiandongshenbaoByShenbaoid(shenbaoid);
    }

    /**
     * 查询专家变动申报列表
     * 
     * @param zhuanjiabiandongshenbao 专家变动申报
     * @return 专家变动申报
     */
    @Override
    public List<Zhuanjiabiandongshenbao> selectZhuanjiabiandongshenbaoList(Zhuanjiabiandongshenbao zhuanjiabiandongshenbao)
    {
        return zhuanjiabiandongshenbaoMapper.selectZhuanjiabiandongshenbaoList(zhuanjiabiandongshenbao);
    }

    /**
     * 新增专家变动申报
     * 
     * @param zhuanjiabiandongshenbao 专家变动申报
     * @return 结果
     */
    @Override
    public int insertZhuanjiabiandongshenbao(Zhuanjiabiandongshenbao zhuanjiabiandongshenbao)
    {
        return zhuanjiabiandongshenbaoMapper.insertZhuanjiabiandongshenbao(zhuanjiabiandongshenbao);
    }

    /**
     * 修改专家变动申报
     * 
     * @param zhuanjiabiandongshenbao 专家变动申报
     * @return 结果
     */
    @Override
    public int updateZhuanjiabiandongshenbao(Zhuanjiabiandongshenbao zhuanjiabiandongshenbao)
    {
        return zhuanjiabiandongshenbaoMapper.updateZhuanjiabiandongshenbao(zhuanjiabiandongshenbao);
    }

    /**
     * 批量删除专家变动申报
     * 
     * @param shenbaoids 需要删除的专家变动申报主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiabiandongshenbaoByShenbaoids(String shenbaoids)
    {
        return zhuanjiabiandongshenbaoMapper.deleteZhuanjiabiandongshenbaoByShenbaoids(Convert.toStrArray(shenbaoids));
    }

    /**
     * 删除专家变动申报信息
     * 
     * @param shenbaoid 专家变动申报主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiabiandongshenbaoByShenbaoid(String shenbaoid)
    {
        return zhuanjiabiandongshenbaoMapper.deleteZhuanjiabiandongshenbaoByShenbaoid(shenbaoid);
    }

    @Override
    public List<Map<String,Object>>selectZhuanjiaBiandongListCustom(String ppDeptId, String ppZhuanjiamingcheng, String ppBiandongleixing, String ppBiandongriqikaishi, String ppBiandongriqijieshu, int ppPageIndex, int ppPageSize){

        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;

        return zhuanjiabiandongshenbaoMapper.selectZhuanjiaBiandongListCustom(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing, ppBiandongriqikaishi, ppBiandongriqijieshu,mmStartIndex,ppPageSize);
    }

    @Override
    public int countZhuanjiaBiandongListCustom(String ppDeptId, String ppZhuanjiamingcheng,String ppBiandongleixing, String ppBiandongriqikaishi, String ppBiandongriqijieshu){
        return zhuanjiabiandongshenbaoMapper.countZhuanjiaBiandongListCustom(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing, ppBiandongriqikaishi, ppBiandongriqijieshu);
    }

    @Override
    @Transactional
    public boolean addCustom(Zhuanjiabiandongshenbao shenbao,
                             ZhuanjiabiandongshenbaoQuxiaozige quxiao,
                             ZhuanjiabiandongshenbaoChuguo chuguo,
                             ZhuanjiabiandongshenbaoLizhi lizhi,
                             ZhuanjiabiandongshenbaoShilian shilian,
                             ZhuanjiabiandongshenbaoTingfa tingfa,
                             ZhuanjiabiandongshenbaoQushi qushi,
                             ZhuanjiabiandongshenbaoHuifu huifu,
                             ZhuanjiabiandongshenbaoZhuanli zhuanli,
                             Zhuanjia zhuanjia,
                             String filepath,
                             String yuanyin){

        boolean ok = true;

        String shenbaoid = UUID.randomUUID().toString();
        String biandongleixing = shenbao.getBiandongleixing();

        System.out.println("aaaaa:"+yuanyin);

        switch (biandongleixing) {
            case "1": //取消享受政府特殊津贴资格
                shenbao.setBiandongshijian(quxiao.getBiandongshijian());
                quxiao.setShenbaoid(shenbaoid);
                quxiao.setYuanyin(yuanyin);
                if (quxiaozigeService.insertZhuanjiabiandongshenbaoQuxiaozige(quxiao) != 1) {
                    ok = false;
                    throw new RuntimeException("添加取消资格失败");
                }

                break;
            case "2": //未经组织同意出国半年以上不归
                shenbao.setBiandongshijian(chuguo.getBiandongshijian());
                chuguo.setShenbaoid(shenbaoid);
                if (chuguoService.insertZhuanjiabiandongshenbaoChuguo(chuguo) != 1) {
                    ok = false;
                    throw new RuntimeException("添加出国失败");
                }

                break;
            case "3": //未经原单位组织批准自动离职
                shenbao.setBiandongshijian(lizhi.getBiandongshijian());
                lizhi.setShenbaoid(shenbaoid);
                if (lizhiService.insertZhuanjiabiandongshenbaoLizhi(lizhi) != 1) {
                    ok = false;
                    throw new RuntimeException("添加离职失败");
                }

                break;
            case "4": //直接联系或通过亲属均无法联系
                shenbao.setBiandongshijian(shilian.getBiandongshijian());
                shilian.setShenbaoid(shenbaoid);
                shilian.setYuanyin(yuanyin);
                if (shilianService.insertZhuanjiabiandongshenbaoShilian(shilian) != 1) {
                    ok = false;
                    throw new RuntimeException("添加失联失败");
                }

                break;
            case "5": //其他原因停发津贴
                shenbao.setBiandongshijian(tingfa.getBiandongshijian());
                tingfa.setShenbaoid(shenbaoid);
                tingfa.setYuanyin(yuanyin);
                if (tingfaService.insertZhuanjiabiandongshenbaoTingfa(tingfa) != 1) {
                    ok = false;
                    throw new RuntimeException("添加停发失败");
                }

                break;
            case "6": //去世
                shenbao.setBiandongshijian(qushi.getBiandongshijian());
                qushi.setShenbaoid(shenbaoid);
                if (qushiService.insertZhuanjiabiandongshenbaoQushi(qushi) != 1) {
                    ok = false;
                    throw new RuntimeException("添加去世失败");
                }

                break;
            case "7": //恢复发放津贴
                shenbao.setBiandongshijian(huifu.getBiandongshijian());
                huifu.setShenbaoid(shenbaoid);
                huifu.setYuanyin(yuanyin);
                if (huifuService.insertZhuanjiabiandongshenbaoHuifu(huifu) != 1) {
                    ok = false;
                    throw new RuntimeException("添加恢复失败");
                }

                break;
            case "8": //人事关系转出
                shenbao.setBiandongshijian(zhuanli.getZhuanchushijian());
                zhuanli.setShenbaoid(shenbaoid);
                if (zhuanliService.insertZhuanjiabiandongshenbaoZhuanli(zhuanli) != 1) {
                    ok = false;
                    throw new RuntimeException("添加转隶失败");
                }

                break;
            case "9": //核增+恢复
                shenbao.setBiandongshijian(huifu.getBiandongshijian());
                huifu.setShenbaoid(shenbaoid);
                huifu.setYuanyin(yuanyin);
                if (huifuService.insertZhuanjiabiandongshenbaoHuifu(huifu) != 1) {
                    ok = false;
                    throw new RuntimeException("添加核增恢复失败");
                }

                String zhuanjiaid = UUID.randomUUID().toString();
                shenbao.setZhuanjiaid(zhuanjiaid);
                shenbao.setXingming(zhuanjia.getXingming());
                zhuanjia.setZhuanjiaid(zhuanjiaid);
                if (zhuanjiaService.insertZhuanjia(zhuanjia) != 1) {
                    ok = false;
                    throw new RuntimeException("添加专家失败");
                }
                break;
        }

        //添加申报
        shenbao.setShenbaoid(shenbaoid);
        shenbao.setXingming(zhuanjiaService.selectZhuanjiaByZhuanjiaid(shenbao.getZhuanjiaid()).getXingming());
        shenbao.setStatus(1l);
        if (zhuanjiabiandongshenbaoMapper.insertZhuanjiabiandongshenbao(shenbao)!=1){
            ok = false;
            throw new RuntimeException("添加申报失败");
        }

        if(filepath!=null){
            Fujian fujian = new Fujian();
            fujian.setId(UUID.randomUUID().toString());
            fujian.setOwnerid(shenbaoid);
            fujian.setWenjianming(filepath.substring(filepath.lastIndexOf("/")+1));
            fujian.setUrl(filepath);
            fujian.setIsvalid(1l);
            fujian.setLeixing("变动");

            if (fujianService.insertFujian(fujian)!=1){
                ok = false;
                throw new RuntimeException("添加证明材料失败");
            }
        }

        //添加事务
        Shiwu mmShiwu = new Shiwu();
        mmShiwu.setShiwuid(UUID.randomUUID().toString());
        mmShiwu.setDeptid(shenbao.getDeptid());
        mmShiwu.setIsqidong(0l);
        mmShiwu.setIsjieshu(0l);
        mmShiwu.setChuangjianshijian(new Date());
        mmShiwu.setBeizhu1(shenbaoid);
        mmShiwu.setIsvalid(1l);
        if (shiwuService.insertShiwu(mmShiwu)!=1){
            ok = false;
            throw new RuntimeException("添加事务失败");
        }

        //删除附件，重新添加

        return ok;
    }

    @Override
    @Transactional
    public boolean modifyCustom(Zhuanjiabiandongshenbao shenbao,
                                ZhuanjiabiandongshenbaoQuxiaozige quxiao,
                                ZhuanjiabiandongshenbaoChuguo chuguo,
                                ZhuanjiabiandongshenbaoLizhi lizhi,
                                ZhuanjiabiandongshenbaoShilian shilian,
                                ZhuanjiabiandongshenbaoTingfa tingfa,
                                ZhuanjiabiandongshenbaoQushi qushi,
                                ZhuanjiabiandongshenbaoHuifu huifu,
                                ZhuanjiabiandongshenbaoZhuanli zhuanli,
                                Zhuanjia zhuanjia,
                                String filepath,
                                String yuanyin){

        boolean ok = true;

        String shenbaoid = shenbao.getShenbaoid();
        String biandongleixing = shenbao.getBiandongleixing();

        //先将旧的变动详情删除
        quxiaozigeService.deleteZhuanjiabiandongshenbaoQuxiaozigeByShenbaoid(shenbaoid);
        chuguoService.deleteZhuanjiabiandongshenbaoChuguoByShenbaoid(shenbaoid);
        lizhiService.deleteZhuanjiabiandongshenbaoLizhiByShenbaoid(shenbaoid);
        shilianService.deleteZhuanjiabiandongshenbaoShilianByShenbaoid(shenbaoid);
        tingfaService.deleteZhuanjiabiandongshenbaoTingfaByShenbaoid(shenbaoid);
        qushiService.deleteZhuanjiabiandongshenbaoQushiByShenbaoid(shenbaoid);
        huifuService.deleteZhuanjiabiandongshenbaoHuifuByShenbaoid(shenbaoid);
        zhuanliService.deleteZhuanjiabiandongshenbaoZhuanliByShenbaoid(shenbaoid);

        //根据新的变动详情添加
        switch (biandongleixing) {
            case "1": //取消享受政府特殊津贴资格
                shenbao.setBiandongshijian(quxiao.getBiandongshijian());
                quxiao.setShenbaoid(shenbaoid);
                quxiao.setYuanyin(yuanyin);
                if (quxiaozigeService.insertZhuanjiabiandongshenbaoQuxiaozige(quxiao) != 1) {
                    ok = false;
                    throw new RuntimeException("添加取消资格失败");
                }

                break;
            case "2": //未经组织同意出国半年以上不归
                shenbao.setBiandongshijian(chuguo.getBiandongshijian());
                chuguo.setShenbaoid(shenbaoid);
                if (chuguoService.insertZhuanjiabiandongshenbaoChuguo(chuguo) != 1) {
                    ok = false;
                    throw new RuntimeException("添加出国失败");
                }

                break;
            case "3": //未经原单位组织批准自动离职
                shenbao.setBiandongshijian(lizhi.getBiandongshijian());
                lizhi.setShenbaoid(shenbaoid);
                if (lizhiService.insertZhuanjiabiandongshenbaoLizhi(lizhi) != 1) {
                    ok = false;
                    throw new RuntimeException("添加离职失败");
                }

                break;
            case "4": //直接联系或通过亲属均无法联系
                shenbao.setBiandongshijian(shilian.getBiandongshijian());
                shilian.setShenbaoid(shenbaoid);
                shilian.setYuanyin(yuanyin);
                if (shilianService.insertZhuanjiabiandongshenbaoShilian(shilian) != 1) {
                    ok = false;
                    throw new RuntimeException("添加失联失败");
                }

                break;
            case "5": //其他原因停发津贴
                shenbao.setBiandongshijian(tingfa.getBiandongshijian());
                tingfa.setShenbaoid(shenbaoid);
                tingfa.setYuanyin(yuanyin);
                if (tingfaService.insertZhuanjiabiandongshenbaoTingfa(tingfa) != 1) {
                    ok = false;
                    throw new RuntimeException("添加停发失败");
                }

                break;
            case "6": //去世
                shenbao.setBiandongshijian(qushi.getBiandongshijian());
                qushi.setShenbaoid(shenbaoid);
                if (qushiService.insertZhuanjiabiandongshenbaoQushi(qushi) != 1) {
                    ok = false;
                    throw new RuntimeException("添加去世失败");
                }

                break;
            case "7": //恢复发放津贴
                shenbao.setBiandongshijian(huifu.getBiandongshijian());
                huifu.setShenbaoid(shenbaoid);
                huifu.setYuanyin(yuanyin);
                if (huifuService.insertZhuanjiabiandongshenbaoHuifu(huifu) != 1) {
                    ok = false;
                    throw new RuntimeException("添加恢复失败");
                }

                break;
            case "8": //人事关系转出
                shenbao.setBiandongshijian(zhuanli.getZhuanchushijian());
                zhuanli.setShenbaoid(shenbaoid);
                if (zhuanliService.insertZhuanjiabiandongshenbaoZhuanli(zhuanli) != 1) {
                    ok = false;
                    throw new RuntimeException("添加转隶失败");
                }

                break;
            case "9": //核增+恢复
                shenbao.setBiandongshijian(huifu.getBiandongshijian());
                huifu.setShenbaoid(shenbaoid);
                huifu.setYuanyin(yuanyin);
                if (huifuService.insertZhuanjiabiandongshenbaoHuifu(huifu) != 1) {
                    ok = false;
                    throw new RuntimeException("添加核增恢复失败");
                }

                String zhuanjiaid = UUID.randomUUID().toString();
                shenbao.setZhuanjiaid(zhuanjiaid);
                shenbao.setXingming(zhuanjia.getXingming());
                zhuanjia.setZhuanjiaid(zhuanjiaid);
                if (zhuanjiaService.insertZhuanjia(zhuanjia) != 1) {
                    ok = false;
                    throw new RuntimeException("添加专家失败");
                }
                break;
        }

        if(filepath!=null) {
            Fujian temp = new Fujian();
            temp.setOwnerid(shenbaoid);
            if (fujianService.selectFujianList(temp).size()==1){
                Fujian fujian = fujianService.selectFujianList(temp).get(0);

                fujian.setWenjianming(filepath.substring(filepath.lastIndexOf("/") + 1));
                fujian.setUrl(filepath);

                if (fujianService.updateFujian(fujian) != 1) {
                    ok = false;
                    throw new RuntimeException("添加证明材料失败");
                }
            }else if(fujianService.selectFujianList(temp).isEmpty()){
                Fujian fujian = new Fujian();
                fujian.setId(UUID.randomUUID().toString());
                fujian.setOwnerid(shenbaoid);
                fujian.setWenjianming(filepath.substring(filepath.lastIndexOf("/")+1));
                fujian.setUrl(filepath);
                fujian.setIsvalid(1l);
                fujian.setLeixing("变动");

                if (fujianService.insertFujian(fujian)!=1){
                    ok = false;
                    throw new RuntimeException("添加证明材料失败");
                }
            }else{
                throw new RuntimeException("添加证明材料失败");
            }

        }

        //修改申报
        if (zhuanjiabiandongshenbaoMapper.updateZhuanjiabiandongshenbao(shenbao)!=1){
            ok = false;
            throw new RuntimeException("修改申报失败");
        }

        //删除附件，重新添加

        return ok;
    }

    @Override
    public Map<String,Object> selectZhuanjiaBiandongCustom(String ppShenbaoId){
        return zhuanjiabiandongshenbaoMapper.selectZhuanjiaBiandongCustom(ppShenbaoId);
    }
}
